//
//  AppDelegate+Tip.m
//  mcrmSAAS
//
//  Created by wjxfhxy on 15/9/28.
//  Copyright (c) 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "AppDelegate+Tip.h"

@interface AppDelegate (TipInternal)

@property(nonatomic,strong) CProgressViewPanelHelper *bTip;
@property(nonatomic,strong) CProgressViewPanelHelper *cTip;

@end

@implementation AppDelegate (TipInternal)

-(void)setBTip:(CProgressViewPanelHelper *)bTip
{
    [self setAssociatedRetainNonatomicValue:bTip withKey:"bTip"];
}

-(CProgressViewPanelHelper*)bTip
{
    return [self associatedValueForKey:"bTip"];
}

-(void)setCTip:(CProgressViewPanelHelper *)cTip
{
    [self setAssociatedRetainNonatomicValue:cTip withKey:"cTip"];
}

-(CProgressViewPanelHelper*)cTip
{
    return [self associatedValueForKey:"cTip"];
}

@end

@implementation AppDelegate (Tip)

+(void)presentBottomAutoCloseTipViewWithTip:(NSString*)tip
{
    [(AppDelegate*)[UIApplication sharedApplication].delegate showAutoCloseTip:tip];
}

+(void)presentCenterTipViewWithTip:(NSString*)tip closeBlock:(void(^)(void(^f)()))closeTipView
{
    [(AppDelegate*)[UIApplication sharedApplication].delegate presentCenterTipViewWithTip:tip closeBlock:closeTipView];
}

- (UIView *)getProgressViewPanelParent:(CProgressViewPanel *)pvp
{
    return self.window.rootViewController.view;
}

- (CGRect)getProgressViewPanelCoverRect:(CProgressViewPanel *)pvp
{
    if (pvp == self.bTip.pvpanel)
    {
        return CGRectMake(0, self.window.rootViewController.view.bottom - 100, self.window.rootViewController.view.width, 30);
    }
    return self.window.rootViewController.view.bounds;
}

-(CGPoint)getProgressViewCenterPosition:(CProgressViewPanel *)pvp
{
    CGRect frame = [self getProgressViewPanelCoverRect:pvp];
    return CGPointMake(frame.size.width / 2, frame.size.height / 2);
}

- (void)touchProgressViewPanel:(CProgressViewPanel *)pvp
{
//    if (pvp == self.bTip.pvpanel)
//    {
//        [self.bTip forceReleaseProgressViewPanel];
//        self.bTip = nil;
//    }
}

- (void)progressViewPanelDidRemove:(CProgressViewPanel *)pvp
{
    if (pvp == self.bTip.pvpanel)
    {
        self.bTip = nil;
    }
    else if (pvp == self.cTip.pvpanel)
    {
        self.cTip = nil;
    }
}

- (void)showAutoCloseTip:(NSString *)tip
{
    __weak id s = self;
    
    if (self.bTip == nil)
        self.bTip = [CProgressViewPanelHelper new];
    self.bTip.delayReleaseTime = 3;
    [self.bTip progressViewPanelHelperWithActivity:NO title:@"" text:tip delegate:s];
    [self.bTip releaseProgressViewPanel];
}

-(void)presentCenterTipViewWithTip:(NSString*)tip closeBlock:(void(^)(void(^f)()))closeTipView
{
    __weak id s = self;
    
    if (self.cTip == nil)
        self.cTip = [CProgressViewPanelHelper new];
    [self.cTip progressViewPanelHelperWithActivity:NO title:tip text:@"" delegate:s];
    
    void(^f)()=^
    {
        [self.cTip releaseProgressViewPanel];
    };
    
    closeTipView(f);
}

@end
